package com.mazihao.emos.aop;

import com.mazihao.emos.common.util.AjaxResult;
import com.mazihao.emos.config.shiro.ThreadLocalToken;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class TokenAspect {

    @Resource
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.mazihao.emos.controller.*.*(..)))")//切点表达式
    public void aspect(){

    }

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        AjaxResult result = (AjaxResult) point.proceed();
        String token = threadLocalToken.getToken();
        if (token != null){
            result.put("token",token);
            threadLocalToken.clear();
        }
        return result;
    }
}
